package com.lin.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lin.entity.Comment;
import com.lin.mapper.CommentMapper;
import com.lin.mapper.StatisticMapper;
import com.lin.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private StatisticMapper statisticMapper;

    /**
     * 根据文章id查询评论数据:分页
     *
     * @param articleId
     * @param pageNum
     * @param count
     * @return
     */
    @Override
    public PageInfo<Comment> findByArticleId(Integer articleId, Integer pageNum, Integer count) {
        PageHelper.startPage(pageNum, count);
        List<Comment> comments = commentMapper.findByArticleId(articleId);
        return new PageInfo<>(comments);
    }

    /**
     * 发布评论
     *
     * @param comment
     */
    @Override
    public void pushComment(Comment comment) {
        //1  添加评论
        commentMapper.addComment(comment);

        //2 更新文章评论量
        statisticMapper.updateCommentsNum(comment.getArticleId());
    }

    @Override
    public Integer getTotalCommentsNum() {
        return commentMapper.getTotalCommentsNum();
    }

    @Override
    public List<Comment> findRecentComments(Integer num) {
        return commentMapper.findRecentComments(num);
    }

    @Override
    public void deleteCommentByArticleId(Integer articleId) {
        commentMapper.deleteCommentByArticleId(articleId);
    }
}
